package com.example.artemis.statistics;

import com.example.artemis.events.EventListener;
import com.example.artemis.events.MessageEvent;
import com.example.artemis.events.MessageFailedEvent;
import com.example.artemis.events.MessageProcessedEvent;
import com.example.artemis.events.MessageSentEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 统计事件监听器
 * 监听消息事件并更新统计数据
 */
public class StatisticsEventListener implements EventListener<MessageEvent> {

    private static final Logger logger = LoggerFactory.getLogger(StatisticsEventListener.class);

    private final StatisticsService statisticsService;

    public StatisticsEventListener(StatisticsService statisticsService) {
        this.statisticsService = statisticsService;
    }

    @Override
    public void handleEvent(MessageEvent event) {
        if (event == null) {
            logger.warn("收到null事件，忽略");
            return;
        }

        try {
            if (event instanceof MessageSentEvent) {
                handleMessageSentEvent((MessageSentEvent) event);
            } else if (event instanceof MessageProcessedEvent) {
                handleMessageProcessedEvent((MessageProcessedEvent) event);
            } else if (event instanceof MessageFailedEvent) {
                handleMessageFailedEvent((MessageFailedEvent) event);
            } else {
                logger.warn("收到未知类型的事件: {}", event.getClass().getSimpleName());
            }
        } catch (Exception e) {
            logger.error("处理事件时发生错误: {}", event.getClass().getSimpleName(), e);
        }
    }

    /**
     * 处理消息发送事件
     */
    private void handleMessageSentEvent(MessageSentEvent event) {
        logger.debug("处理消息发送事件: {}", event.getMessageId());

        statisticsService.recordMessageSent(
                event.getMessageId(),
                event.getQueueName(),
                event.getProducerId(),
                event.getProductionTime(),
                event.getMessageSize()
        );
    }

    /**
     * 处理消息处理事件
     */
    private void handleMessageProcessedEvent(MessageProcessedEvent event) {
        logger.debug("处理消息处理事件: {}", event.getMessageId());

        statisticsService.recordMessageProcessed(
                event.getMessageId(),
                event.getQueueName(),
                event.getConsumerId(),
                event.getProcessingTime(),
                event.getDeliveryAttempt(),
                event.getMessageAge()
        );
    }

    /**
     * 处理消息失败事件
     */
    private void handleMessageFailedEvent(MessageFailedEvent event) {
        logger.debug("处理消息失败事件: {}", event.getMessageId());

        statisticsService.recordMessageFailed(
                event.getMessageId(),
                event.getQueueName(),
                event.getConsumerId(),
                event.getProcessingTime(),
                event.getDeliveryAttempt(),
                event.getErrorMessage(),
                event.getFailureType().name(),
                event.isWillRetry()
        );
    }

    @Override
    public Class<MessageEvent> getEventType() {
        return MessageEvent.class;
    }

    @Override
    public String getListenerName() {
        return "StatisticsEventListener";
    }
}